package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MatrCodeBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.MatrCodeMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;
/**
 * 物料编码-编辑
 * 
 * @author 廖大富
 *
 */
public class MatrCodeUpdate implements ServerInterface {

	private static Logger logger = LogManager.getLogger(MatrCodeUpdate.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			MatrCodeMapper mapper = factory.getMapper(MatrCodeMapper.class);
			MatrCodeBean bean = JsonUtils.objectToPojo(req.getData(), MatrCodeBean.class);
			// 校验字段重复
			if (mapper.check2("matr_no", bean.getMatrNo(), bean.getId()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "材料编号：" + bean.getMatrNo() + "的信息已存在。", void.class);
			}
			if (mapper.check2("matr_name", bean.getMatrName(), bean.getId()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "材料名称：" + bean.getMatrName() + "的信息已存在。", void.class);
			}
			if (StringUtils.isNullOrEmpty(bean.getId())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "id为空", void.class);
			}
			bean.setUpdateInfo(userInfo);
			mapper.updateMatrCode(bean);
		} catch (Exception e) {
			logger.error("物料编码修改失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "物料编码修改失败", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}

}
